package com.example.library_common.view.video_view;

import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.library_common.R;
import com.example.library_common.utils.QMUIDisplayHelper;
import com.example.library_common.view.video_view.utils.VideoViewControllerListener;
import com.example.library_common.view.video_view.utils.VideoViewUtils;

import java.text.MessageFormat;

public class VideoViewBottomControllerView extends LinearLayout implements View.OnClickListener {


    //视频播放与暂停
    private ImageButton mVideoViewPlayBtn;

    //视频时长与正在播放时刻
    private TextView mVideoViewTimer;

    //视频进度拖动条
    private SeekBar mVideoViewSeekProgress;

    //视频屏幕切换
    private ImageButton mVideoViewScreenBtn;

    //视频进度条
    private ProgressBar mVideoViewBottomProgress;

    private VideoViewControllerListener mVideoViewControllerListener;

    public void setVideoViewControllerListener( VideoViewControllerListener mVideoViewControllerListener) {
        this.mVideoViewControllerListener = mVideoViewControllerListener;
    }

    public VideoViewBottomControllerView(Context context) {
        this(context, null);
    }

    public VideoViewBottomControllerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VideoViewBottomControllerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.video_view_bottom_controller_view_layout, this);

        mVideoViewPlayBtn = findViewById(R.id.mVideoViewPlayBtn);

        mVideoViewTimer = findViewById(R.id.mVideoViewTimer);

        mVideoViewSeekProgress = findViewById(R.id.mVideoViewSeekProgress);

        mVideoViewScreenBtn = findViewById(R.id.mVideoViewScreenBtn);

        mVideoViewBottomProgress = findViewById(R.id.mVideoViewBottomProgress);

        mVideoViewPlayBtn.setOnClickListener(this);

        mVideoViewScreenBtn.setOnClickListener(this);

        mVideoViewScreenBtn.setSelected(QMUIDisplayHelper.isPortrait());

    }

    public void startingPlay() {
        if (mVideoViewPlayBtn != null) {
            mVideoViewPlayBtn.setSelected(true);
        }
    }

    public void stopPlay() {
        if (mVideoViewPlayBtn != null) {
            mVideoViewPlayBtn.setSelected(false);
        }
    }

    public void setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener l) {
        if (mVideoViewSeekProgress == null) return;
        mVideoViewSeekProgress.setOnSeekBarChangeListener(l);
    }

    public void setTime(long current, long duration) {
        if (mVideoViewTimer == null) {
            return;
        }

        mVideoViewTimer.setText(
                MessageFormat
                        .format("{0}/{1}",
                                VideoViewUtils.stringForTime(current),
                                VideoViewUtils.stringForTime(duration)
                        )
        );
    }

    public void setProgress(float progress) {
        if (mVideoViewBottomProgress != null) {
            mVideoViewBottomProgress.setProgress((int) progress);
        }

        if (mVideoViewSeekProgress != null) {
            mVideoViewSeekProgress.setProgress((int) progress);
        }
    }

    public void setBufferProgress(float bufferProgress) {
        if (mVideoViewBottomProgress != null) {
            mVideoViewBottomProgress.setSecondaryProgress((int) bufferProgress);
        }

        if (mVideoViewSeekProgress != null) {
            mVideoViewSeekProgress.setSecondaryProgress((int) bufferProgress);
        }
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mVideoViewScreenBtn.setSelected(QMUIDisplayHelper.isPortrait());
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.mVideoViewPlayBtn) {
            handleVideoPlayingStatus();
        } else if (id == R.id.mVideoViewScreenBtn) {
            handleVideoViewScreen();
        }
    }

    //处理视频横竖屏切换
    private void handleVideoViewScreen() {
        boolean mVideoPlayingStatus = !mVideoViewScreenBtn.isSelected();
        mVideoViewScreenBtn.setSelected(mVideoPlayingStatus);
        if (mVideoViewControllerListener != null) {
            mVideoViewControllerListener.onScreenSwitch();
        }
    }

    //处理视频播放与暂停
    private void handleVideoPlayingStatus() {

        if (mVideoViewControllerListener != null) {
            mVideoViewControllerListener.onVideoPlayingStatusChange(mVideoViewPlayBtn.isSelected());
        }
    }

}
